package com.collect.senegal.zhiScene.zhiLogin;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.senegal.MyApplication;
import com.collect.senegal.R;
import com.collect.senegal.zhiBase.BaseActivity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRAppVersionEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRCheckInOutEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRLoginEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRUserCallRecordWarnEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISResetPasswordEntity;
import com.collect.senegal.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;
import com.collect.senegal.zhiScene.zhiWidget.zhiInputView.ZHIInputEditView;
import com.collect.senegal.zhiScene.zhiWidget.zhiStepView.ZHINextStepView;
import com.collect.senegal.zhiUtils.ToastUtil;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_LOGIN_FORGOT_PASSWORD)
public class ZHILoginResetPasswordActivity extends BaseActivity<ZHILoginPresenter> implements ZHILoginContact.View {

    @BindView(R.id.ei_alfp_login_account)
    ZHIInputEditView ei_alfp_login_account;

    @BindView(R.id.ei_alfp_login_pwd)
    ZHIInputEditView ei_alfp_login_pwd;

    @BindView(R.id.v_alfp_line_0)
    View v_alfp_line_0;

    @BindView(R.id.v_alfp_line_1)
    View v_alfp_line_1;

    @BindView(R.id.iv_alfp_back)
    ImageView iv_alfp_back;

    @BindView(R.id.btn_alfp_negtive)
    ZHINextStepView btn_alfp_negtive;

    @BindView(R.id.tv_alfp_mobile)
    TextView tv_alfp_mobile;


    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_forgot_password;
    }

    @Override
    protected void initView() {
        try {
            tv_alfp_mobile.setText(ZHILoginReduce.mAccount);

            ei_alfp_login_account.setOnTextDidChangedListener(this::checkBtnCanCommit);
            ei_alfp_login_account.setOnTextDidChangedListener(this::checkBtnCanCommit);

            ei_alfp_login_pwd.setPasswordInputFilter();

            iv_alfp_back.setOnClickListener(v -> finish());

            btn_alfp_negtive.setOnMenuItemDidChangedListener(item -> {
                if (checkCanCommit()) {
                    ZHISResetPasswordEntity zhisResetPasswordEntity = new ZHISResetPasswordEntity(ZHILoginReduce.mAccount, ei_alfp_login_account.getTextTrimAll(), ei_alfp_login_pwd.getTextTrimAll());
                    assert mPresenter != null;
                    mPresenter.resetPassword(ZHILoginResetPasswordActivity.this, zhisResetPasswordEntity);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetErrorInfo(String methodName) {
        Log.d(TAG, "onGetErrorInfo: " + methodName);
//        String resetPassword = "resetPassword";
//        if (methodName.equalsIgnoreCase(resetPassword)) {
//            //弹框提示
//            ZHIDialogMessage.showDialog(getActivity(),
//                    null,
//                    "The new password is between 8-15 characters and must contain 1 number, 1 lower case character, 1 uppercase character",
//                    true,
//                    tag -> {
//
//                    });
//        }
    }

    @Override
    public void onGetLoginInfoV2(ZHIRLoginEntity zhiRLoginEntity) {

    }

    @Override
    public void onGetUserCallRecordWarn(ZHIRUserCallRecordWarnEntity zhirUserCallRecordWarnEntity) {

    }

    @Override
    public void onResetPassword() {
        if (ei_alfp_login_pwd != null) {
            ZHILoginReduce.mNewPassword = ei_alfp_login_pwd.getTextTrimAll();
            finish();
        }
    }

    @Override
    public void onGetAppVersionInfoV2(ZHIRAppVersionEntity ftrAppVersionEntity) {

    }

    @Override
    public void onGetCheckState(ZHIRCheckInOutEntity zhirCheckInOutEntity) {
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean checkCanCommit() {

        hideKeyboardNow(tv_alfp_mobile);

        if (ei_alfp_login_account.getTextTrimAll().trim().length() < 1) {
            ToastUtil.s(ei_alfp_login_account.getHintText());

            v_alfp_line_0.setBackgroundColor(getResources().getColor(R.color.theme_red_0));
            ei_alfp_login_account.setBeFocusing();
            return false;
        }

        v_alfp_line_0.setBackgroundColor(getResources().getColor(R.color.pay_line_color));

        if (!ei_alfp_login_pwd.checkPasswordInputValid()) {
//            ToastUtil.s("The new password is between 8-15 characters and must contain 1 number, 1 lower case character, 1 uppercase character");

            v_alfp_line_1.setBackgroundColor(getResources().getColor(R.color.theme_red_0));
            ei_alfp_login_pwd.setBeFocusing();

            //弹框提示
            ZHIDialogMessage.showDialog(getActivity(),
                    null,
                    MyApplication.getAppContext().getString(R.string.the_new_password_tips),
                    true,
                    tag -> {

                    });
            return false;
        }

        v_alfp_line_0.setBackgroundColor(getResources().getColor(R.color.pay_line_color));
        v_alfp_line_1.setBackgroundColor(getResources().getColor(R.color.pay_line_color));

        return true;
    }

    private void checkBtnCanCommit() {
        if (ei_alfp_login_account.getTextTrimAll().trim().length() < 1) {
            return;
        }

        if (ei_alfp_login_pwd.getTextTrimAll().trim().length() < 1) {
            return;
        }

        v_alfp_line_0.setBackgroundColor(getResources().getColor(R.color.pay_line_color));
        v_alfp_line_1.setBackgroundColor(getResources().getColor(R.color.pay_line_color));
    }
}